package demo.utils;

import demo.using.TempRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author : GuoFei
 * @date : 2019/1/15 14:24
 */
public class ThreadUtil {

    private static final Logger logger = LoggerFactory.getLogger(ThreadUtil.class);

    public static void main(String[] args) {
        logger.info("begin");
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        List<Integer> ids = Arrays.asList(1, 2, 3, 4, 6, 7, 8, 9, 10, 15, 20);
        for (Integer id : ids) {
            executorService.execute(() -> doSomeThing(id));
        }
        executorService.shutdown();
        logger.info("shutdown");
        try {
            // 返回等待参数时间后，判断当前线程池内子线程是否已经全部执行完成
            boolean finish = executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("end");
    }

    public static void doSomeThing(int times){
        sleep(times);
    }

    public static void sleep(int seconds){
        try {
            Thread.sleep(seconds*1000);
            logger.info(seconds+":done");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
